package com.wx.disparent.mallparent.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.disparent.mallparent.commons.inface.IBaseService;
import com.wx.disparent.mallparent.commons.util.MD5Utils;
import com.wx.disparent.mallparent.dao.AdminUserMapper;
import com.wx.disparent.mallparent.entity.ProblemEntity;
import com.wx.disparent.mallparent.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AdminUserService implements IBaseService<UserEntity> {
    @Override
    public UserEntity findById(int id) {
        UserEntity byId = adminUserMapper.findById(id);
        return byId;
    }

    @Override
    public int insert(UserEntity userEntity) {
        userEntity.setPasswd(MD5Utils.stringToMD5("111111"));
        int insert = adminUserMapper.insert(userEntity);
        return insert;
    }

    @Override
    public int update(UserEntity userEntity) {
        return 0;
    }

    @Override
    public int deleteById(int t) {
        return 0;
    }

    @Autowired
    private AdminUserMapper adminUserMapper;

    public List<UserEntity> findAll(Map<String, Object> map) {
        List<UserEntity> list = adminUserMapper.findAll(map);
        return list;
    }

    public Map<String, Object> login(String nameOrPhone, String passwd) {

        String passwdMD5 = MD5Utils.stringToMD5(passwd);

        HashMap<String, String> loginMap = new HashMap<>();
        loginMap.put("nameOrPhone", nameOrPhone);
        loginMap.put("passwd", passwdMD5);
        UserEntity userEntity1 = adminUserMapper.findByLoginMap(loginMap);
        HashMap<String, Object> map = new HashMap<>();
        if (userEntity1 == null) {
            map.put("isSuccess", "false");
            map.put("message", "用户名或手机号或密码错误");
            map.put("userEntity", null);
        } else {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String lastLogin = sf.format(new Date());
            Integer id = userEntity1.getId();
            HashMap<String, Object> lastMap = new HashMap<>();
            lastMap.put("id",id);
            lastMap.put("lastLogin",lastLogin);
            Integer updateLastLogin = adminUserMapper.updateLastLogin(lastMap);
            UserEntity byId = adminUserMapper.findById(id);
            map.put("isSuccess", "true");
            map.put("message", null);
            map.put("userEntity", byId);
        }
        return map;
    }

    public Map<String, Object> userInfoCheck(UserEntity userEntity) {
        Map<String, Object> map = new HashMap<>();
        UserEntity userNameEntity = null;
        if (StringUtils.isNotBlank(userEntity.getUsername())) {
            userNameEntity = adminUserMapper.findByUserName(userEntity.getUsername());
        }

        UserEntity byId = adminUserMapper.findById(userEntity.getId());
        if (userNameEntity == null || userEntity.getUsername().equals(byId.getUsername())) {
            map.put("userNameCheck", "true");
            map.put("userNameMessage", null);
        } else {
            map.put("userNameCheck", "false");
            map.put("userNameMessage", "用户名重复");
        }

        UserEntity emailEntity = adminUserMapper.findByEmail(userEntity.getEmail());
        if (emailEntity == null || userEntity.getEmail().equals(byId.getEmail())) {
            map.put("emailCheck", "true");
            map.put("emailMessage", null);
        } else {
            map.put("emailCheck", "false");
            map.put("emailMessage", "电子邮箱已被注册");
        }
        return map;
    }

    public PageInfo findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> map = new HashMap<>();
        List<UserEntity> list = adminUserMapper.findAll(map);

        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public PageInfo<UserEntity> search(int pageSize, int pageNum, String searchContext) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserEntity> searchList = adminUserMapper.search(searchContext);
        PageInfo<UserEntity> searchPageInfo = new PageInfo<>(searchList);
        return searchPageInfo;
    }

    public List<UserEntity> search(String searchContext) {
        List<UserEntity> searchList = adminUserMapper.search(searchContext);
        return searchList;
    }

    public UserEntity findUserEntityById(Integer id) {
        UserEntity userEntity = adminUserMapper.findUserEntityById(id);
        return userEntity;
    }

    public Map<String, Object> updatePwCheck(UserEntity userEntity) {
        UserEntity byId = adminUserMapper.findById(userEntity.getId());
        Map<String, Object> map = new HashMap<>();

        if(byId.getPasswd().equals(MD5Utils.stringToMD5(userEntity.getPasswd()))){
            map.put("pwCheck","true");
            map.put("pwMessage",null);
        }else{
            map.put("pwCheck","false");
            map.put("pwMessage","旧密码输入错误，请确认后输入");
        }
        return map;
    }

    public Integer updatePassWdById(UserEntity userEntity) {
        userEntity.setPasswd(MD5Utils.stringToMD5(userEntity.getPasswd()));
        return adminUserMapper.updatePassWdById(userEntity);
    }

    public Integer resetPw(Integer id) {
        String pw = MD5Utils.stringToMD5("111111");
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("pw", pw);
        Integer integer = adminUserMapper.resetPw(map);
        System.out.println(id+"***************"+adminUserMapper.findById(id).getPasswd()+"*****************");
        return integer;
    }

    public List<UserEntity> findReferUsers(Integer referId) {
        UserEntity byId = adminUserMapper.findById(referId);
        List<UserEntity> referList = new ArrayList<>();
        referList.add(byId);
        return referList;
    }

    public List<UserEntity> findLowerUsers(Integer id) {
        List<UserEntity> lowerList = adminUserMapper.findByReferId(id);
        return lowerList;
    }

    public List<ProblemEntity> findAllProblem() {

        List<ProblemEntity> problemEntityList = adminUserMapper.findAllProblem();
        return problemEntityList;

    }

    public Map<String, Object> updateCheck(UserEntity userEntity) {
        Map<String, Object> map = new HashMap<>();
        UserEntity userNameEntity = null;
        if (StringUtils.isNotBlank(userEntity.getUsername())) {
            userNameEntity = adminUserMapper.findByUserName(userEntity.getUsername());
        }
        UserEntity byId = adminUserMapper.findById(userEntity.getId());
        if (userNameEntity == null || userEntity.getUsername().equals(byId.getUsername())) {
            map.put("userNameCheck", "true");
            map.put("userNameMessage", null);
        } else {
            map.put("userNameCheck", "false");
            map.put("userNameMessage", "用户名重复");
        }
        return map;
    }

    public Integer updateById(UserEntity userEntity) {
        Integer i =adminUserMapper.updateById(userEntity);
        return i;
    }

    public Map<String, Object> addCheck(UserEntity userEntity) {
        Map<String, Object> map = new HashMap<>();

        UserEntity userNameEntity = null;
        if (StringUtils.isNotBlank(userEntity.getUsername())) {
            userNameEntity = adminUserMapper.findByUserName(userEntity.getUsername());
        }
        if (userNameEntity == null) {
            map.put("userNameCheck", "true");
            map.put("userNameMessage", null);
        } else {
            map.put("userNameCheck", "false");
            map.put("userNameMessage", "用户名重复");
        }

        UserEntity phoneEntity = null;
        if(StringUtils.isNotBlank(userEntity.getPhone())){
            phoneEntity = adminUserMapper.findByPhone(userEntity.getPhone());
        }
        if (phoneEntity == null) {
            map.put("phoneCheck", "true");
            map.put("phoneMessage", null);
        } else {
            map.put("phoneCheck", "false");
            map.put("phoneMessage", "手机号重复");
        }


        UserEntity emailEntity = null;
        if(StringUtils.isNotBlank(userEntity.getEmail())){
            emailEntity = adminUserMapper.findByEmail(userEntity.getEmail());
        }
        if (emailEntity == null) {
            map.put("emailCheck", "true");
            map.put("emailMessage", null);
        } else {
            map.put("emailCheck", "false");
            map.put("emailMessage", "邮箱重复");
        }
        return map;

    }

    public List<UserEntity> lowerSearch(String searchContext, Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("searchContext",searchContext);
        map.put("id",id);
        List<UserEntity> searchList = adminUserMapper.lowerSearch(map);
        return searchList;
    }

    public List<UserEntity> referSearch(String searchContext, Integer referId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("searchContext",searchContext);
        map.put("referId",referId);
        List<UserEntity> searchList = adminUserMapper.referSearch(map);
        return searchList;
    }

}
